home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 5.7 KB | 247 lines | [TEXT/MPS ] |
- /*
- File: GMTTime.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __GMTTIME__
- #include "GMTTime.h"
- #endif
-
- #ifndef __OSUTILS__
- #include "OSUtils.h"
- #endif
-
- #ifndef __SCRIPT__
- #include "Script.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __TIME_H__
- #include "Time.h"
- #endif
-
- #ifndef __ABSTRACTFILE__
- #include "AbstractFile.h"
- #endif
-
- #ifndef __IOSTREAM__
- #include "IOStream.h"
- #endif
-
- #pragma segment TimeStamp
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TTimeStamp::TTimeStamp ():
- fOriginTime ( GetLocalTime () ),
- fOriginZone ( GetLocalZone () )
- {
- }
-
- /***********************************|****************************************/
-
- TTimeStamp::~TTimeStamp ()
- {
- }
-
- /***********************************|****************************************/
-
- TTimeStamp::operator unsigned long () const
- {
- return fOriginTime;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTimeStamp::operator == ( const TTimeStamp& that ) const
- {
- return
- ( fOriginTime + ( fOriginZone * 60 ) ) ==
- ( that.fOriginTime + ( that.fOriginZone * 60 ) );
- }
-
- /***********************************|****************************************/
-
- TTimeStamp&
- TTimeStamp::operator = ( const TTimeStamp& that )
- {
- fOriginTime = that.fOriginTime;
- fOriginZone = that.fOriginZone;
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTimeStamp::operator != ( const TTimeStamp& that ) const
- {
- return !operator == ( that );
- }
-
- /***********************************|****************************************/
-
- void
- TTimeStamp::SetToLocalTime ()
- {
- fOriginZone = GetLocalZone ();
- fOriginTime = GetLocalTime ();
- }
-
- /***********************************|****************************************/
-
- void
- TTimeStamp::ConvertToZone ( TimeZone newZone )
- {
- fOriginTime += GetZoneDifference ( newZone, fOriginZone );
- fOriginZone = newZone;
- }
-
- /***********************************|****************************************/
-
- Time
- TTimeStamp::GetLocalTime ()
- {
- Time localTime;
- ::GetDateTime ( &localTime );
- return localTime;
- }
-
- /***********************************|****************************************/
-
- TimeZone
- TTimeStamp::GetLocalZone ()
- {
- return GetLocalOffsetEastOfGMTInSeconds () / 60;
- }
-
- /***********************************|****************************************/
-
- Time
- TTimeStamp::GetZoneDifference ( const TimeZone local, const TimeZone reference )
- {
- return 60 * (Time) ( local - reference );
- }
-
- /***********************************|****************************************/
-
- char*
- TTimeStamp::CreateString () const
- {
- unsigned long length = GetStringLength ();
- char* string = new char [ length + 1 ];
- MakeString ( string, length );
- return string;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TTimeStamp::GetStringLength () const
- {
- return ::strlen ( ::ctime ( &( (TTimeStamp*) this )->fOriginTime ) ) + 1;
- }
-
- /***********************************|****************************************/
-
- void
- TTimeStamp::MakeString ( char* buffer, unsigned long bufferSize ) const
- {
- ::strncpy ( buffer, ::ctime ( &( (TTimeStamp*) this )->fOriginTime ), (int) bufferSize );
- buffer [ strlen ( buffer ) - 1 ] = 0;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTimeStamp::WriteTo ( TAbstractFile& file ) const
- {
- if ( file.WriteDataIgnore ( &( (TTimeStamp*) this )->fOriginTime, sizeof ( fOriginTime ) ) != noErr )
- return false;
-
- return file.WriteDataIgnore ( &( (TTimeStamp*) this )->fOriginZone, sizeof ( fOriginZone ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TTimeStamp::ReadFrom ( TAbstractFile& file )
- {
- if ( file.ReadDataIgnore ( &fOriginTime, sizeof ( fOriginTime ) ) != noErr )
- return false;
-
- return file.ReadDataIgnore ( &fOriginZone, sizeof ( fOriginZone ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TTimeStamp::operator >> ( ostream& file ) const
- {
- file << "TTimeStamp:" << (void*) this << "\n";
- file << "\tfOriginTime:" << fOriginTime << "\n";
- file << "\tfOriginZone:" << fOriginZone << "\n";
- file.flush ();
- return file;
- }
-
- /***********************************|****************************************/
-
- unsigned long GetCurrentGMTSeconds ()
- {
- unsigned long localSeconds;
- GetDateTime ( &localSeconds );
- return localSeconds - GetLocalOffsetEastOfGMTInSeconds ();
- }
-
- /***********************************|****************************************/
-
- unsigned long ConvertGMTSecondsToLocalSeconds ( unsigned long gmtSeconds )
- {
- return gmtSeconds + GetLocalOffsetEastOfGMTInSeconds ();
- }
-
- /***********************************|****************************************/
-
- unsigned long ConvertLocalSecondsToGMTSeconds ( unsigned long localSeconds )
- {
- return localSeconds + GetLocalOffsetEastOfGMTInSeconds ();
- }
-
- /***********************************|****************************************/
-
- long GetLocalOffsetEastOfGMTInSeconds ()
- {
- static long gDelta = -1; // impossible real value represents “uninitialized”
-
- if ( gDelta == -1 )
- {
- MachineLocation location;
- ::ReadLocation ( &location );
-
- gDelta = 0x00FFFFFF & location.gmtFlags.gmtDelta;
-
- if ( gDelta & 0x00800000 ) // sign extend if negative
- gDelta |= 0xFF000000;
-
- if ( location.gmtFlags.dlsDelta ) // adjust for daylight savings
- gDelta -= 3600;
- }
-
- return gDelta;
- }
-
- /***********************************|****************************************/
-